/*     */ package com.zimbra.cs.wiki;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.service.ServiceException.Argument;
/*     */ import com.zimbra.common.util.MapUtil;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Document;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.UserServlet;
/*     */ import com.zimbra.cs.service.doc.DocServiceException;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.io.IOException;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class WikiPage
/*     */ {
/*     */   protected long mTimestamp;
/*     */   protected String mAccountId;
/*     */   protected String mWikiWord;
/*     */   protected int mId;
/*     */   protected int mFolderId;
/*     */   protected int mVersion;
/*     */   protected long mCreatedDate;
/*     */   protected long mModifiedDate;
/*     */   protected String mCreator;
/*     */   protected String mModifiedBy;
/*     */   protected String mFragment;
/*     */   protected String mContents;
/*     */   
/*     */   public static class WikiContext
/*     */   {
/*     */     OperationContext octxt;
/*     */     AuthToken auth;
/*     */     String view;
/*     */     Locale locale;
/*     */     
/*     */     public WikiContext(OperationContext o, AuthToken a)
/*     */     {
/*  55 */       this.octxt = o;this.auth = a;this.view = null;
/*     */     }
/*     */     
/*  58 */     public WikiContext(OperationContext o, AuthToken a, String v) { this.octxt = o;this.auth = a;this.view = v;
/*     */     }
/*     */     
/*  61 */     public WikiContext(OperationContext o, AuthToken a, String v, Locale l) { this.octxt = o;this.auth = a;this.view = v;this.locale = l;
/*     */     }
/*     */   }
/*     */   
/*     */   public static WikiPage create(Document page) {
/*  66 */     return new LocalWikiPage(page);
/*     */   }
/*     */   
/*     */   public static WikiPage create(String accountId, String path) throws ServiceException {
/*  70 */     return new RemoteWikiPage(accountId, path);
/*     */   }
/*     */   
/*     */   public WikiPage() {
/*  74 */     this.mTimestamp = System.currentTimeMillis();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public abstract String getContents(WikiContext paramWikiContext)
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public abstract String getFolderKey();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized WikiTemplate getTemplate(WikiContext ctxt)
/*     */     throws ServiceException
/*     */   {
/*  94 */     return new WikiTemplate(getContents(ctxt), this.mAccountId, getFolderKey(), this.mWikiWord);
/*     */   }
/*     */   
/*  97 */   public long getTimestamp() { return this.mTimestamp; }
/*     */   
/*     */   public String getAccountId() {
/* 100 */     return this.mAccountId;
/*     */   }
/*     */   
/* 103 */   public String getWikiWord() { return this.mWikiWord; }
/*     */   
/*     */   public long getLastVersion()
/*     */   {
/* 107 */     return this.mVersion;
/*     */   }
/*     */   
/*     */   public long getCreatedDate() {
/* 111 */     return this.mCreatedDate;
/*     */   }
/*     */   
/*     */   public long getModifiedDate() {
/* 115 */     return this.mModifiedDate;
/*     */   }
/*     */   
/*     */   public String getCreator() {
/* 119 */     return this.mCreator;
/*     */   }
/*     */   
/*     */   public String getLastEditor() {
/* 123 */     return this.mModifiedBy;
/*     */   }
/*     */   
/*     */   public String getFolderId() {
/* 127 */     ItemId iid = new ItemId(this.mAccountId, this.mFolderId);
/* 128 */     return iid.toString((String)null);
/*     */   }
/*     */   
/*     */   public String getId() {
/* 132 */     ItemId iid = new ItemId(this.mAccountId, this.mId);
/* 133 */     return iid.toString((String)null);
/*     */   }
/*     */   
/*     */   public String getFragment() {
/* 137 */     return this.mFragment;
/*     */   }
/*     */   
/* 140 */   private static Map<String, WikiPage> sPageCache = MapUtil.newLruMap(1024);
/* 141 */   private static long TTL = 600000L;
/*     */   
/*     */   private static WikiPage getCachedTemplate(Domain domain, String template) {
/* 144 */     long now = System.currentTimeMillis();
/* 145 */     String key = domain.getId() + template;
/* 146 */     WikiPage page = null;
/* 147 */     synchronized (sPageCache) {
/* 148 */       page = (WikiPage)sPageCache.get(key);
/* 149 */       if ((page != null) && (page.getTimestamp() + TTL < now)) {
/* 150 */         sPageCache.remove(key);
/* 151 */         page = null;
/*     */       }
/*     */     }
/* 154 */     return page;
/*     */   }
/*     */   
/*     */   private static void cacheTemplate(Domain domain, String template, WikiPage page) {
/* 158 */     if (page == null)
/* 159 */       return;
/* 160 */     String key = domain.getId() + template;
/* 161 */     synchronized (sPageCache) {
/* 162 */       sPageCache.put(key, page);
/*     */     }
/*     */   }
/*     */   
/* 166 */   public static WikiPage findTemplate(WikiContext ctxt, String accountId, String template) throws ServiceException { Provisioning prov = Provisioning.getInstance();
/* 167 */     Account acct = prov.get(Key.AccountBy.id, accountId, ctxt.auth);
/* 168 */     Domain domain = prov.getDomain(acct);
/* 169 */     template = "/Template/" + template;
/*     */     
/* 171 */     WikiPage page = null;
/* 172 */     if (domain != null) {
/* 173 */       page = getCachedTemplate(domain, template);
/* 174 */       if (page != null) {
/* 175 */         return page;
/*     */       }
/* 177 */       String domainWiki = domain.getAttr("zimbraNotebookAccount", null);
/* 178 */       if (domainWiki != null) {
/* 179 */         page = findTemplatePage(ctxt, domainWiki, template);
/*     */       }
/*     */     }
/* 182 */     if (page == null) {
/* 183 */       String defaultWiki = prov.getConfig().getAttr("zimbraNotebookAccount", null);
/* 184 */       if (defaultWiki != null) {
/* 185 */         page = findTemplatePage(ctxt, defaultWiki, template);
/*     */       }
/*     */     }
/* 188 */     if (domain != null) {
/* 189 */       cacheTemplate(domain, template, page);
/*     */     }
/* 191 */     return page;
/*     */   }
/*     */   
/*     */ 
/* 195 */   public static WikiPage missingPage(String template) { return new ErrorPage(template); }
/*     */   
/*     */   private static WikiPage findTemplatePage(WikiContext ctxt, String wikiAccountName, String template) throws ServiceException {
/* 198 */     Provisioning prov = Provisioning.getInstance();
/* 199 */     Account acct = prov.get(Key.AccountBy.name, wikiAccountName);
/*     */     
/* 201 */     if (acct == null) {
/* 202 */       throw ServiceException.FAILURE("wiki account " + wikiAccountName + " does not exist, please check " + "zimbraNotebookAccount" + " on the domain or global config", null);
/*     */     }
/*     */     
/* 205 */     WikiPage page = null;
/* 206 */     if (Provisioning.onLocalServer(acct)) {
/*     */       try {
/* 208 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
/* 209 */         MailItem item = mbox.getItemByPath(ctxt.octxt, template);
/* 210 */         if ((item instanceof Document)) {
/* 211 */           page = create((Document)item);
/*     */         }
/*     */       } catch (ServiceException se) {}
/*     */     } else {
/* 215 */       page = create(acct.getId(), template);
/*     */       try {
/* 217 */         page.getContents(ctxt);
/*     */       } catch (ServiceException se) {
/* 219 */         page = null;
/*     */       }
/*     */     }
/* 222 */     return page;
/*     */   }
/*     */   
/*     */   public static class LocalWikiPage extends WikiPage {
/*     */     LocalWikiPage(Document doc) {
/* 227 */       addWikiItem(doc);
/*     */     }
/*     */     
/*     */     private void addWikiItem(Document newItem) {
/* 231 */       this.mWikiWord = newItem.getName();
/* 232 */       this.mId = newItem.getId();
/* 233 */       this.mVersion = newItem.getVersion();
/* 234 */       this.mModifiedDate = newItem.getDate();
/* 235 */       this.mModifiedBy = newItem.getCreator();
/* 236 */       this.mCreatedDate = newItem.getDate();
/* 237 */       this.mCreator = newItem.getCreator();
/* 238 */       this.mFolderId = newItem.getFolderId();
/* 239 */       this.mFragment = newItem.getFragment();
/* 240 */       this.mContents = null;
/* 241 */       this.mAccountId = newItem.getMailbox().getAccountId();
/*     */     }
/*     */     
/*     */     public Document getWikiRevision(WikiPage.WikiContext ctxt, int version) throws ServiceException {
/* 245 */       Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(this.mAccountId);
/* 246 */       return (Document)mbox.getItemRevision(ctxt.octxt, this.mId, MailItem.Type.DOCUMENT, version);
/*     */     }
/*     */     
/*     */     public String getContents(WikiPage.WikiContext ctxt) throws ServiceException
/*     */     {
/* 251 */       if (this.mContents == null) {
/*     */         try {
/* 253 */           byte[] raw = getWikiRevision(ctxt, this.mVersion).getContent();
/* 254 */           this.mContents = new String(raw, "UTF-8");
/*     */         } catch (IOException ioe) {
/* 256 */           throw DocServiceException.ERROR("can't get contents", ioe);
/*     */         }
/*     */       }
/* 259 */       return this.mContents;
/*     */     }
/*     */     
/*     */     public String getFolderKey()
/*     */     {
/* 264 */       return Integer.toString(this.mFolderId);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class RemoteWikiPage extends WikiPage {
/*     */     private String mPath;
/*     */     private String mRestUrl;
/*     */     private String mContents;
/*     */     
/*     */     RemoteWikiPage(String accountId, String path) throws ServiceException {
/* 274 */       Provisioning prov = Provisioning.getInstance();
/* 275 */       Account acct = prov.getAccountById(accountId);
/* 276 */       this.mRestUrl = (UserServlet.getRestUrl(acct) + path);
/*     */     }
/*     */     
/*     */     public String getContents(WikiPage.WikiContext ctxt) throws ServiceException
/*     */     {
/* 281 */       if (this.mContents != null)
/* 282 */         return this.mContents;
/*     */       AuthToken auth;
/*     */       AuthToken auth;
/* 285 */       if ((ctxt != null) && (ctxt.auth != null)) {
/* 286 */         auth = ctxt.auth;
/*     */       } else {
/*     */         try {
/* 289 */           auth = AuthProvider.getAdminAuthToken();
/*     */         } catch (Exception ate) {
/* 291 */           auth = null;
/*     */         }
/*     */       }
/* 294 */       String url = this.mRestUrl + "?fmt=native&disp=attachment";
/* 295 */       Pair<Header[], byte[]> resource = UserServlet.getRemoteResource(auth.toZAuthToken(), url);
/* 296 */       int status = 0;
/* 297 */       for (Header h : (Header[])resource.getFirst())
/* 298 */         if (h.getName().compareTo("X-Zimbra-Http-Status") == 0)
/* 299 */           status = Integer.parseInt(h.getValue());
/* 300 */       if (status != 200)
/* 301 */         throw ServiceException.RESOURCE_UNREACHABLE("http error " + status, null, new ServiceException.Argument[0]);
/*     */       try {
/* 303 */         this.mContents = new String((byte[])resource.getSecond(), "UTF-8");
/*     */       } catch (IOException ioe) {
/* 305 */         throw ServiceException.RESOURCE_UNREACHABLE("invalid url", ioe, new ServiceException.Argument[0]);
/*     */       }
/* 307 */       return this.mContents;
/*     */     }
/*     */     
/*     */     public String getFolderKey()
/*     */     {
/* 312 */       return this.mPath;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class ErrorPage extends WikiPage {
/*     */     String template;
/*     */     
/* 319 */     ErrorPage(String template) { this.template = template; }
/*     */     
/*     */ 
/*     */     public String getContents(WikiPage.WikiContext ctxt)
/*     */     {
/* 324 */       return "<!-- missing template" + this.template + " -->";
/*     */     }
/*     */     
/*     */     public String getFolderKey()
/*     */     {
/* 329 */       return "";
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/wiki/WikiPage.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */